using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemlc : MonoBehaviour
{
    public float speed = 5f; // 敌人移动速度
    private Transform player; // 玩家对象的Transform

    void Start()
    {
        // 查找Tag为"Player"的对象
        GameObject playerObject = GameObject.FindGameObjectWithTag("Player");
        if (playerObject != null)
        {
            player = playerObject.transform;
        }
        else
        {
            Debug.LogError("未找到Tag为'Player'的对象！");
        }
    }

    void Update()
    {
        if (player != null)
        {
            // 敌人朝玩家方向移动
            Vector3 direction = (player.position - transform.position).normalized;

            // 移动敌人
            transform.position += direction * speed * Time.deltaTime;

            // 使敌人的Z轴始终面向玩家
            Vector3 lookDirection = player.position - transform.position;
            lookDirection.y = 0; // 保持敌人水平旋转，不改变Y轴
            if (lookDirection != Vector3.zero)
            {
                transform.rotation = Quaternion.LookRotation(lookDirection);
            }
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        // 检测是否与玩家碰撞
        if (collision.gameObject.CompareTag("Player"))
        {
            Debug.Log("敌人碰撞到玩家！");
            // 在这里可以添加碰撞后的逻辑，例如减少玩家生命值
        }
    }
}